import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Receive } from 'src/entity/Receive'
import { Task } from 'src/entity/Task'
import { Connection, IsNull, Repository } from 'typeorm'
import { v4 as uuidV4 } from 'uuid'

@Injectable()
export class ReceiveService {
  constructor(
    @InjectRepository(Receive)
    private receiveRepository: Repository<Receive>,
    private connection: Connection,
  ) { }

  async add(data) {
    const receiveCode = 'R' + uuidV4()
    data.receiveCode = receiveCode
    data.receiveType = '0'

    await this.connection.transaction(async (manager) => {
      const sqlData = await this.receiveRepository.save(data)
      const taskData = new Task()
      taskData.receiveBranch = sqlData.receiveBranch
      taskData.receiveBranchId = sqlData.receiveBranchId
      taskData.receivePerson = sqlData.receivePerson
      taskData.receivePersonId = sqlData.receivePersonId
      taskData.receiveCode = sqlData.receiveCode
      taskData.receiveDate = sqlData.receiveDate
      taskData.taskName = sqlData.receivePerson + '的领用申请'
      taskData.taskStatus = sqlData.receiveType
      taskData.taskType = '2'
      await manager.insert(Task, taskData)
    })

    return data
  }

  async del(id: number) {
    await this.connection.transaction(async (manager) => {
      const sqlData = await this.receiveRepository.findOne({
        where: {
          id,
        },
      })
      await manager.update(
        Task,
        {
          receiveCode: sqlData.receiveCode,
        },
        {
          isDelete: true,
        },
      )
      await manager.update(
        Receive,
        {
          id: id,
        },
        {
          isDelete: true,
        },
      )
    })
  }

  async update(data) {
    data.receiveType = '0'
    data.reason = null
    data.getDate = null
    let sqlData
    await this.connection.transaction(async (manager) => {
      await manager.update(
        Task,
        {
          receiveCode: data.receiveCode,
        },
        {
          taskStatus: data.receiveType,
          reason: null,
          taskDate: null,
          taskPersonId: null,
          taskPerson: null,
        },
      )
      sqlData = await this.receiveRepository.update(
        {
          id: data.id,
        },
        data,
      )
    })
    return sqlData
  }

  // 更新某个属性
  async updateProperty(code, property, value) {
    return await this.receiveRepository.update(
      {
        receiveCode: code,
      },
      {
        [property]: value,
      },
    )
  }

  // 分页查询
  async getPageList({
    pageRequest: { pageIndex, pageSize },
    receiveCode,
    receivePerson,
    receiveBranch,
    getPerson,
    getBranch,
    receiveType,
    orgId,
  }) {
    const sqlAllData = await this.receiveRepository.find({
      where: {
        isDelete: IsNull(),
        receiveBranchId: orgId,
        receiveCode: receiveCode,
        receivePerson: receivePerson,
        receiveBranch: receiveBranch,
        getPerson: getPerson,
        getBranch: getBranch,
        receiveType: receiveType,
      },
    })
    const sqlData = await this.receiveRepository.find({
      where: {
        isDelete: IsNull(),
        receiveBranchId: orgId,
        receiveCode: receiveCode,
        receivePerson: receivePerson,
        receiveBranch: receiveBranch,
        getPerson: getPerson,
        getBranch: getBranch,
        receiveType: receiveType,
      },
      skip: (pageIndex - 1) * pageSize,
      take: pageSize,
      withDeleted: false,
    })

    return {
      content: sqlData.map((item) => {
        return {
          ...item,
          typeId: item.typeId.toString(),
        }
      }),
      total: sqlAllData.length,
    }
  }

  // 获取领用详情 purchaseCode -> *
  async getReceiveDetail(code) {
    const sqlData = await this.receiveRepository.findOne({
      where: {
        receiveCode: code,
        isDelete: IsNull(),
      },
    })
    return sqlData
  }
}
